<?php

namespace xiaoshao\weixin;

class Wxapp
{
    //微信小程序登陆第一步，需要现在后台微信配置 小程序配置里面配置好参数
    public function codeToInfo($code,$appid,$secret)
    {

        $result = $this->code_to_sessionkey($appid, $secret, $code);
        
        return $result;
         
    }
    //微信小程序登陆第二步，根据微信端传过来的值解析用户数据
    public function getWxInfo($appid, $edata, $iv,$session_key)
    {
        $result = $this->decrypt($edata, $iv, $session_key);
        
        if (!$result['status']) {
            return $result;
        }
        
        if( $result['data']['watermark']['appid'] != $appid )
        {
            return ['status'=>false,'msg'=>'appid错误'];
        }
        
        return $result;
        
    }
    /**
     * 一般小程序功能
     * 根据前台传过来的code 去取openid和session_key
     * @param $code
     * @return array
     */
    private function code_to_sessionkey($appid,$secret,$code)
    {
        $result = array(
            'status' => false,
            'data' => '',
            'msg' => ''
        );
        
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
       
        $re = $this->get($url);
        $re = json_decode($re,true);
        
        if(!isset($re['errcode'])){
            $result['data'] = $re;
            $result['status'] = true;
        }else{
            $result['msg'] = $re['errcode'].":".$re['errmsg'];
        }

        return $result;
    }

    private function get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus  = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
    
    //解密
    private function decrypt($edata,$iv,$sessionKey)
    {
        $result = array(
            'status' => false,
            'data' => '',
            'msg' => ''
        );
        
        if (strlen($sessionKey) != 24) {
            $result['msg'] = 'sessionKey不正确';
            return $result;
        }
        
        $aesKey=base64_decode($sessionKey);
        
        
        if (strlen($iv) != 24) {
            $result['msg'] = 'iv不正确';
            return $result;
        }
        
        $aesIV=base64_decode($iv);
        
        $aesCipher=base64_decode($edata);
        
        $re=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        
        $data=json_decode($re,true);
        
        if( $data)
        {
            $result['status'] = true;
            $result['data'] = $data;
            return $result;
        }else{
            $result['msg'] = '解密错误';
            return $result;
        }
    }
}
